<script>
var selectedFiles = new Array();

var $=null, flow=null, form=null, element=null, layer=null, upload=null;
layui.use(['jquery', 'flow', 'form', 'element', 'layer', 'upload'], function() {
	$=layui.$, flow=layui.flow, form=layui.form, element=layui.element, layer=layui.layer, upload=layui.upload;

	### 获取根文件夹
	getList("go");

	//左侧导航栏收缩提示
	$('#ID_btn_upload').hover(function(){
		layer.tips('上传的文件最好不要包含中文及中文标点符号', '#ID_btn_upload', {tips:[4,'#FF8000'],time:0});
	},function(){
		layer.closeAll('tips');
	});

});

### --------------- 以上是操作监听脚本 ---------------
### --------------- 以下是操作执行脚本 ---------------

### action=回退，go=去到某个文件夹，reload=刷新
### folder=指定的文件夹
### filter=类型过滤，可以是file、image、video、audio、zip、doc、folder
function getList(action, folder){
	var data = {action : action, folder : (folder ? folder : $("#ID_folder").val()), accept: '#(accept)'};
	layer.load(1);
	$.post("#(ctx)/manage/files/getList", data, function(result){
        $("#ID_file_list_div").html(result);
    	### 加载完成后需要Render
    	element.render("breadcrumb");
    	form.render();
    	flow.lazyimg({
    	    elem: '#ID_files img'
    	});
    }).always(function() {
    	layer.closeAll('loading');
    });
}

### 删除文件
function delFiles(){
	var data = {folder: $("#ID_folder").val(), size: 0, files: []};
	$("input:checkbox:checked").each(function(){
		console.log(this);
		data.files[data.size] = $(this).attr("file");
		data.size++;
	});
	if(data.size==0){
		layer.alert("您没有勾选任何项目！", {icon: 0});
		return;
	}
	layer.confirm('此操作将物理删除已选项目，确定删除吗？', {icon: 3, title:'提示'}, function(index){
		$.ajax({ ### 用Ajax替换弹窗中的提交
			url : "#(ctx)/manage/files/ajaxDel",
			type : "post",
			data : data,
			beforeSend : function(){layer.load(1);}, ### 防止重复操作
			complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
			success : function(result){
				layer.msg("操作成功");
				if(result.code == 0){
					if(result.tip){
						layer.alert(result.tip, {icon: 1});
					}
					reload();
				} else {
					layer.alert("操作失败：" + result.msg, {icon: 2});
				}
			},
			error : function (e) { ### Ajax调用失败
				layer.alert("操作失败：" + e.responseText, {icon: 2});
			}
		});

		layer.close(index);
	});
}

### 新建文件夹
function newFolder(){
	layer.prompt({title:"请输入新文件夹名称（请不要用中文名称）"}, function(value, index, elem){
		$.ajax({ ### 用Ajax替换弹窗中的提交
			url : "#(ctx)/manage/files/ajaxNewFolder",
			type : "post",
			data : {folder: $("#ID_folder").val(), folderName: value},
			beforeSend : function(){layer.load(1);}, ### 防止重复操作
			complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
			success : function(result){
		        if(result.code != 0){
		        	layer.alert(result.msg);
		        }
			},
			error : function (e) { ### Ajax调用失败
				layer.alert("操作失败：" + e.responseText, {icon: 2});
			}
		});
		layer.close(index);
		reload();
	});
}

### 刷新
function reload(){
	getList("reload");
}

### 回到上一文件夹
function back(){
	getList("back");
}

### 点击文件
function selectedItem(file_elem, changeCheckState){
	var selectMode = $("#ID_select_mode").val();
	if(!selectMode)return;

	if(!$("#ID_multiple").val()){
		$(".selected").removeClass("selected");
	}
	if($(file_elem).hasClass("selected")){
		$(file_elem).removeClass("selected");
	} else {
		$(file_elem).addClass("selected");
	}
}

function ckSelectFiles(){
	$(".selected").each(function(o){
		$(this).find("input").each(function(){
			var fileUrl = "#(ctx)#(UPLOAD_PATH)"+ $("#ID_folder").val() + "/" + $(this).attr("file");
			returnFileUrl(fileUrl);
			return;
		});
	});
}

function getUrlParam( paramName ) {
    var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
    var match = window.location.search.match( reParam );

    return ( match && match.length > 1 ) ? match[1] : null;
}
// Simulate user action of selecting a file to be returned to CKEditor.
function returnFileUrl(fileUrl) {
    var funcNum = getUrlParam( 'CKEditorFuncNum' );
    window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl );
    window.close();
}

</script>